package controller;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.LoginCredentials;
import model.Staff;


import BizLogic.BadDataException;
import BizLogic.PasswordGenerator;
import BizLogic.StaffManager;

import dao.LoginCredentialsDAO;
import dao.StaffDAO;

import dao.mysql.LoginCredentialsDAOImpl;
import dao.mysql.StaffDAOImpl;


/**
 * Servlet implementation class CreateStaffServlet
 */
@WebServlet("/CreateStaffServlet")
public class CreateStaffServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CreateStaffServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			createStaff(request,response);
		} catch (BadDataException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			createStaff(request,response);
		} catch (BadDataException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	String error=null;
	
	private String Validate(HttpServletRequest request, HttpServletResponse response){
		
		if(request.getParameter("staffID") == null)
		{error="Staff ID missing";}
		if(request.getParameter("email") == null)
		{error="Email missing";}

		if(request.getParameter("phone") == null)
		{error="Phone Number missing";}

		if(request.getParameter("name") == null)
		{error="Name missing";}

		
		

		
		return error;
	}
	
	private void createStaff(HttpServletRequest request, HttpServletResponse response)throws BadDataException, IOException{

		HttpSession session=request.getSession(false);
		if(session!=null){
		

	
		Staff userDTO=new Staff();
		LoginCredentials loginDTO=new LoginCredentials();
		

		
		
		
		userDTO.setStaffID(request.getParameter("staffID"));
		userDTO.setName(request.getParameter("name"));
		userDTO.setEmail(request.getParameter("email"));
		userDTO.setPhoneNumber(request.getParameter("phone"));
		userDTO.setReportTo("");
		
		loginDTO.setStaffID(request.getParameter("staffID"));
		loginDTO.setRole(request.getParameter("role"));
		loginDTO.setSecurityQuestion(" ");
		loginDTO.setSecurityAnswer(request.getParameter(" "));
		//loginDTO.setPassword(request.getParameter("staffID"));
//		loginDTO.setSalt(passgen.getsalt());
		
			Validate(request,response);
		
		try {
			
			
			if(request.getParameter("hide").toString().equalsIgnoreCase("insert"))
			{
				System.out.println("hide");
				StaffManager manage=new StaffManager();
			manage.addRecord(userDTO, loginDTO);

			}
			
			else if(request.getParameter("hide").toString().equalsIgnoreCase("update"))
			{
				StaffManager manage=new StaffManager();
				
				manage.updateRecord(userDTO, loginDTO);	
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		if(error!=null)throw new BadDataException(error);
		
		else{
			
			
			RequestDispatcher rd=request.getRequestDispatcher("LoadStaffServlet");
			try {
				rd.forward(request, response);
			} catch (ServletException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		}
		
		else{
			response.sendRedirect("Login.jsp");
		}
	}
	

}
